package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Employee;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/*
    用于处理与员工相关的功能
 */
@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService es;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee , HttpSession session){

        System.out.println("employee = " + employee);

        //1. 调用service
        Employee loginEmployee = es.login(employee);

        //2. 返回结果
        if(loginEmployee != null){
            //登录成功

            //2.1  把员工对象保存到session作用域
            session.setAttribute("employee" , loginEmployee);

            //2.2 返回查询到的该员工的信息给页面
            return R.success(loginEmployee);

        }else{
            //登录失败
            return R.error("用户名或者密码错误66666666！");
        }

    }


    /**
     * 退出登录
     *    只需要把session里面保存的该用户数据给清空即可
     * @param  session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){

        try {
            //把登录成功的时候，保存的员工数据给删除掉
            //session.removeAttribute("employee");

            //直接把session给干掉
            session.invalidate();

            return R.success("退出成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败！");
        }

    }


    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee , HttpSession session){

        System.out.println("employee = " + employee);
        System.out.println("EmployeeController 的add方法 =====================" + Thread.currentThread().getId());


        //0. 补充数据:: 从页面提交过来的数据不完整，缺少： 密码、创建时间、更新时间、创建用户、更新用户
        String pwd = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(pwd);
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //谁执行了登录，谁就执行了添加操作。
        //Employee e = (Employee) session.getAttribute("employee");
        //employee.setCreateUser(e.getId());
        //employee.setUpdateUser(e.getId());

        //1. 调用service
        int row = es.add(employee);

        //2. 响应
        if(row > 0){
            return R.success("添加员工成功！");
        }else{
            return R.error("添加员工失败！");
        }
    }


    /**
     * 员工分页
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R page(PageParam pageParam){

        //1. 调用service
        IPage<Employee> page = es.page(pageParam);

        //2. 响应结果
        return R.success(page);

    }


    /**
     * 更新员工
     * @param employee
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee , HttpSession session){


        //0. 由于执行的是更新操作，所以要修改update_user 和 update_time 这里两个属性的值
        //Employee e = (Employee) session.getAttribute("employee");

        //employee.setUpdateUser(e.getId());
        //employee.setUpdateTime(LocalDateTime.now());


        //1. 调用service
        int row = es.update(employee);

        //2. 响应结果
        if(row > 0){
            return R.success("更新成功！");
        }else{
            return R.error("更新失败！");
        }
    }


}
